var express = require('express');
// 引入远程接口的调用方法
const ordersApi = require("../api/orders");
// 创建了一个路由器
var router = express.Router();
const _ = require("lodash");


// 路由作用：和页面相关的操作
// 获取订单列表
router.get('/', async function (req, res, next) {
    // 获取浏览器发送的值type为GET，在query接收
    const { curPage, eachPage } = req.query;
    console.log(curPage, eachPage);
    const data = await ordersApi.get({ curPage, eachPage });
    res.send(data)
});

router.get('/findOne/:_id', async function (req, res, next) {
    // params去做路径解析
    const { _id } = req.params;
    const data = await ordersApi.findOne(_id);
    res.send(data)
});

//新增订单
router.post('/add', async function (req, res, next) {
    const { userId, commoditiesId, time,  state, price,orderInfo,count} = req.body;
    const data = await ordersApi.addOrders({ userId, commoditiesId, time,  state, price,orderInfo,count});
    res.send(data)
});

// 删除订单
router.post("/remove", async function (req, res, next) {
    const { _id } = req.body;
    console.log(_id);
    const data = await ordersApi.removeOrders({ _id });
    res.send({ status: true });
})

// 根据订单状态查询订单
router.get("/findOrdersToState", async function (req, res, next) {
    const { _id,state } =  req.query;
    // console.log(_id,state);
    const data = await ordersApi.findOrdersToState({ _id,state });
    res.send(data);
  })

// 暴露接口
module.exports = router;
